﻿using WLFramework.DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SampleSite
{
    public class SYS_GetOrgModel : sys_Org
    {
        public int Depth { get; set; }
        public string ShowOrder { get; set; }
    }

    public class SYS_ModuleByApp
    {
        public int Depth { get; set; }
        public int M_ID { get; set; }
        public int M_ParentID { get; set; }
        public string M_CName { get; set; }
        public string M_MoudleFormUrl { get; set; }
        public string M_Icon { get; set; }
    }

    public class SYS_GetRoles
    {
        public int R_ID { get; set; }
        public string R_CName { get; set; }
        public string R_EName { get; set; }
        public string R_Description { get; set; }
        public int R_State { get; set; }
        public int IsHave { get; set; }
    }

    public class SYS_ApplicationByUserID
    {
        public int A_ID { get; set; }
        public string A_CName { get; set; }
        public string A_EName { get; set; }
        public string A_AppDescription { get; set; }

    }


    // SYS_GetOrg
    public abstract class PermissionCommand : DataAccessor
    {
        [SprocName("SYS_GetOrg")]
        public abstract List<SYS_GetOrgModel> SYS_GetOrg();

        [SprocName("SYS_Create_Org")]
        public abstract sys_ErrorMsg Create_Org(int O_ParentID, string O_CName, string O_EName);

        [SprocName("SYS_CREATE_ModuleList")]
        public abstract sys_ErrorMsg CreateModule(
            int @M_AppID,
            int @M_ParentID,
            string @M_CName,
            string @M_EName,
            string @M_MoudleFormUrl,
            bool @M_IsEdit,
            string @M_Icon,
            int @M_Attr);

        [SprocName("SYS_CREATE_UserInfo")]
        public abstract sys_ErrorMsg CreateUser(
            string @U_LoginNO,
            string @U_Cname,
            string @U_EName,
            string @U_IDCard,
            int @U_Sex,
            DateTime @U_BirthDay,
            string @U_HomeTel,
            string @U_MobileNO,
            string @U_Email,
            string @U_CompanyMail,
            string @U_PhotoUrl,
            string @U_WorkStartDate,
            string @U_WorkEndDate);

        [SprocName("SYS_UPDATE_UserInfo")]
        public abstract sys_ErrorMsg UpdateUser(
            int @U_ID,
            string @U_Cname,
            string @U_EName,
            string @U_IDCard,
            int @U_Sex,
            DateTime @U_BirthDay,
            string @U_HomeTel,
            string @U_MobileNO,
            string @U_Email,
            string @U_CompanyMail,
            string @U_PhotoUrl,
            string @U_WorkStartDate,
            string @U_WorkEndDate);

        [SprocName("SYS_CREATE_ORG")]
        public abstract sys_ErrorMsg CreateOrg(
            int @O_ParentID,
            string @O_Cname,
            string @O_EName);

        [SprocName("SYS_UPDATE_Dep")]
        public abstract sys_ErrorMsg UpdateDep(
            int @O_ID,
            string @O_Cname,
            string @O_EName);

        [SprocName("SYS_ModuleByApplication")]
        public abstract List<SYS_ModuleByApp> GetModuleByApplication(int @A_ID);

        [SprocName("SYS_CREATE_Position")]
        public abstract sys_ErrorMsg CreatePosition(
            int @P_Org,
            string @P_CName,
            string @P_EName,
            int @P_Level);

        [SprocName("SYS_UPDATE_Position")]
        public abstract sys_ErrorMsg UpdatePosition(
            int @P_ID,
            int @P_Org,
            string @P_Cname,
            string @P_EName,
            int @P_Level);

        [SprocName("SYS_UPDATE_ModuleList")]
        public abstract sys_ErrorMsg UpdateModule(
            int @M_ID,
            string @M_CName,
            string @M_EName,
            string @M_MoudleFormUrl,
            bool @M_IsEdit,
            string @M_Icon);

        [SprocName("SYS_User_State")]
        public abstract sys_ErrorMsg UpdateUserState(
            int @U_ID,
            int @U_State);

        [SprocName("SYS_CREATE_Roles")]
        public abstract sys_ErrorMsg CreateRole(
            string @R_CName,
            string @R_EName,
            string @R_Description);

        [SprocName("SYS_Roles_State")]
        public abstract sys_ErrorMsg UpdateRoleState(
            int @R_ID,
            int @R_State);

        [SprocName("SYS_Add_UserPosition")]
        public abstract sys_ErrorMsg UpdateUserPosition(
            int @U_ID,
            int @P_ID);

        [SprocName("SYS_GetRoleByPosition")]
        public abstract List<SYS_GetRoles> GetRoleByPosition(int @P_ID);

        [SprocName("SYS_GetRoleByUser")]
        public abstract List<SYS_GetRoles> GetRoleByUser(int @U_ID);

        [SprocName("SYS_Add_PostRole")]
        public abstract sys_ErrorMsg UpdatePostRole(
            int @P_ID,
            int @R_ID);

        [SprocName("SYS_Add_UserRole")]
        public abstract sys_ErrorMsg UpdateUserRole(
            int @U_ID,
            int @R_ID);

        [SprocName("SYS_ApplicationByUserID")]
        public abstract List<sys_Applications> GetApplicationByUserID(int @U_ID);

        [SprocName("SYS_UserPermissionByUIDAndAID")]
        public abstract List<sys_ModuleList> GetUserPermissionByUIDAndAID(int @U_ID, int @A_ID);
        
    }

    #region 读写分离辅助类
    public enum CommandMethod
    {
        Read,
        Write
    }

    public abstract class ReadCommand : DataAccessor
    {
    }

    public abstract class WriteCommand : DataAccessor
    {
    }

    public class CommandHelper : IDisposable
    {
        public const string ReadConfiguration = "Default";
        public const string WriteConfiguration = "Default";

        private CommandMethod method;
        public CommandMethod Method
        {
            get { return method; }
        }

        private PermissionContext context;
        public PermissionContext Context
        {
            get { return context; }
        }

        public CommandHelper(CommandMethod method)
        {
            this.method = method;
            switch (method)
            {
                case CommandMethod.Read:
                    context = new PermissionContext(ReadConfiguration);
                    break;

                case CommandMethod.Write:
                    context = new PermissionContext(WriteConfiguration);
                    break;
            }
        }

        public ReadCommand CreateReadCommand()
        {
            return DataAccessor.CreateInstance<ReadCommand>(this.context);
        }

        public WriteCommand CreateWriteCommand()
        {
            return DataAccessor.CreateInstance<WriteCommand>(this.context);
        }

        public void Dispose()
        {
            if (context != null)
                context.Dispose();
        }
    }
    #endregion
}